<template>
    <div class="keep-back-view">
        <KeepBack :show="true" @close="handleClose" />
    </div>
</template>

<script setup>
import KeepBack from "@/components/KeepBack.vue";
import { useRouter } from "vue-router";
import { onMounted } from "vue";

const router = useRouter();

const handleClose = () => {
    const prevRouteRaw = sessionStorage.getItem("prevRoute");
    if (prevRouteRaw) {
        let prevRoute = null;
        try {
            prevRoute = JSON.parse(prevRouteRaw);
        } catch {
            prevRoute = null;
        }
        if (prevRoute && prevRoute.fullPath) {
            router.replace(prevRoute.fullPath);
            return;
        }
    }
    router.replace("/");
};

onMounted(() => {
    try {
        localStorage.setItem(
            "havedShowKeepBack",
            JSON.stringify({ value: "true", expiry: Date.now() + 86400000 })
        );
    } catch (e) {
        console.warn("Failed to persist havedShowKeepBack flag", e);
    }
});
</script>

<style scoped>
.keep-back-view {
    width: 100%;
    height: 100%;
}
</style>
